package org.ogv.usada;

import org.ogv.CAssetManager;
import org.ogv.OGV;
import org.ogv.statemanager.State;
import org.ogv.statemanager.StateMgr;
import org.ogv.usada.models.GP;
import org.ogv.usada.states.GameOverScreen;
import org.ogv.usada.states.GameScreen;
import org.ogv.usada.states.LevelSelectScreen;
import org.ogv.usada.states.LoadingScreen;
import org.ogv.usada.states.SplashScreen;
import org.ogv.usada.states.StartScreen;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class UsadaApp implements ApplicationListener {	
	
	private StateMgr _stateMgr;

	@Override
	public void create() {
		//Initializes game engine services
		OGV.asset = new CAssetManager();
		SpriteBatch sb = new SpriteBatch();
		
		//TODO Initializes always on resource
		OGV.asset.load(GP.$().ASSETS_RESIDENT);
		OGV.asset.load(GP.$().ASSETS_NONGAME);
		OGV.asset.finishLoading();
		
		//Create states in game
		_stateMgr = new StateMgr(new State[] {
			new GameScreen(sb),
			new StartScreen(sb),
			new GameOverScreen(sb),
			new LevelSelectScreen(sb),
			new SplashScreen(sb)
		});
		
		//Go to first state
		_stateMgr.requestPushState(GP.$().STATE_SPLASH);
		//_stateMgr.requestPushState(new LoadingScreen(_stateMgr, sb, null, GP.$().ASSETS_NONGAME, GP.$().STATE_START));
		
		//FIXME just test
		//LevelDef ld = Content.$().levels[0];
		//_stateMgr.pushValueInExchangeBuff(new Integer(0));
		//_stateMgr.pushValueInExchangeBuff(ld);
		//_stateMgr.requestPushState(new LoadingScreen(_stateMgr, sb, null, ld.assetDecl, GP.$().STATE_GAME));
		//_stateMgr.requestPushState(new LoadingScreen(_stateMgr, sb, null, GP.$().ASSETS_GAMEOVER, GP.$().STATE_GAMEOVER));
	}

	@Override
	public void resize(int width, int height) {}

	@Override
	public void render() {
		_stateMgr.run(Gdx.graphics.getDeltaTime());
		if(!_stateMgr.hasActiveState()) dispose();
	}

	@Override
	public void pause() {}

	@Override
	public void resume() {}

	@Override
	public void dispose() {
		OGV.asset.dispose();
		System.exit(0);
	}

}
